/**
 * @param {string} S
 * @return {string}
*/
var compressString = function(S) {
  if(!S || S.length == 1) return S;
  let count = 1, result = "";
  for(let i = 1; i < S.length; i++) {
    if(S[i] != S[i - 1]) {
      result += S[i - 1] + count.toString();
      if(count != 1)  count = 1;
    } else {
      count++;
    }
  }
  result += S[S.length - 1] + count.toString();
  if(result.length >= S.length) return S;
  return result;
};
let s = "aabcccccaaa";
console.log(compressString(s));
s = "abbccd";
console.log(compressString(s));
s = "bb";
console.log(compressString(s));
